<?php
/** 
 * @package AMFramework
 * @subpackage Core
 * @author Marcu Alexandru
 * @version 1.0 $Build 305b  
 */
defined('_INDEX') or die('Restricted');

class Amfw_URI 
{
	/*
	 * function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
	 * 
	 */
/*	public function root($fullPath = 1) {
		$pageUrl = 'http';
		if($_SERVER['HTTPS'] == 'on') $pageUrl .= 's';
		$pageUrl .= '://';
		if($_SERVER['SERVER_PORT'] != '80') {
			$pageUrl .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
		} else $pageUrl .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

		return $pageUrl;
	}*/
	
	public static function root() {
		
		$pageUrl = 'http';
		if(@$_SERVER['HTTPS'] == 'on') $pageUrl .= 's';
		$pageUrl .= '://';
		if($_SERVER['SERVER_PORT'] != '80') {
			$pageUrl .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'];
		} else $pageUrl .= $_SERVER['SERVER_NAME'];
		$pageUrl .= '/'.BASE_DIR.'/';
		
		return $pageUrl;		
	}

	public function encodetoUrl( $array, $file = 'index.php' ) {
		$tmp = Amfw_Request::get('get');
		// Stergem din GET ceea ce nu se trece in Url (**build2)
		foreach($tmp as $key => $item) {
			if(!is_string($item)):
				unset($tmp[$key]);
				endif;
		}
		
		// Unim ceea ce deja este in GET cu ceea ce vrem sa adaugam in Url;
		$req = array_merge($tmp, $array);
		$tmp = '';
		$url = self::root() . $file . '?' ;
		foreach ($req as $name => $value) {
			if (!empty( $name ) && !empty( $value )):
				$tmp .= $name . '=' . urlencode($value) .'&'  ;
				endif;
		}
		$tmp = rtrim($tmp,'&');
		$tmp = htmlentities( $tmp );
		# !!needs: Mai trebuie studiata problema / vazut cum sa fac codarea in url cat mai safe
		$url .= $tmp;
		
		return $url;
	}

}


//array_walk($y , create_function('&$v,$k', '$v = $k."=".$v ;'));
//"index.php?".htmlentities(urlencode(implode("&",$y)), ENT_QUOTES);